package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjCustomerInfo;
import com.zb.entity.vo.CustomerAccountVo;
import com.zb.entity.vo.CustomerUsersVo;
import com.zb.mapper.ZjCustomerInfoMapper;
import com.zb.service.ZjCustomerInfoService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商家信息表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-07
 */
@Service
public class ZjCustomerInfoServiceImpl extends ServiceImpl<ZjCustomerInfoMapper, ZjCustomerInfo> implements ZjCustomerInfoService {

    // 更改企业用户数
    @Override
    public void customerUsers(CustomerUsersVo usersVo) {
        Assert.isTrue(null != usersVo.getCustAccIdList() && !usersVo.getCustAccIdList().isEmpty(), "至少要选择一个企业！", null);
        QueryWrapper<ZjCustomerInfo> wrapper = new QueryWrapper<>();
        wrapper.in("id", usersVo.getCustAccIdList());
        List<ZjCustomerInfo> infos = this.list(wrapper);

        List<ZjCustomerInfo> customerInfos = new ArrayList<>();
        ZjCustomerInfo info = null;
        for (ZjCustomerInfo cu : infos) {
            info = new ZjCustomerInfo();
            info.setId(cu.getId());
            info.setUsers(usersVo.getUsers());
            customerInfos.add(info);
        }
        this.updateBatchById(customerInfos);
        // TODO 交易记录
    }

    // 更改企业账户信息
    @Override
    public void customerAccount(CustomerAccountVo accountVo) {
        Assert.isTrue(null != accountVo.getCustAccIdList() && !accountVo.getCustAccIdList().isEmpty(), "至少要选择一个企业！", null);

        QueryWrapper<ZjCustomerInfo> wrapper = new QueryWrapper<>();
        wrapper.in("id", accountVo.getCustAccIdList());
        List<ZjCustomerInfo> infos = this.list(wrapper);
        if (null == infos || infos.isEmpty()) {
            return;
        }

        List<ZjCustomerInfo> customerInfos = new ArrayList<>();
        ZjCustomerInfo info = null;
        for (ZjCustomerInfo cu : infos) {
            info = new ZjCustomerInfo();
            info.setId(cu.getId());
            info.setAmountBalance(cu.getAmountBalance().add(accountVo.getAmountBalance()));
            info.setSmsBalance(cu.getSmsBalance() + accountVo.getSmsBalance());
            customerInfos.add(info);
        }
        this.updateBatchById(customerInfos);
        // TODO 交易记录
    }

}
